<?php
class Con_common_model extends MY_Model {
	function __construct() {
		parent::__construct('con_common');
	}
	
	/**
     * 公用查询程序
     * $condition['id'] = ?
     * $condition['type'] = ?
     * $condition['year'] = ?
     * $condition['name'] = ?
     * $condition['content'] = ?
     * $condition['sort_year'] = ?
     * $condition['sort_create_time'] = ?
     * $condition['sort_update_time'] = ?
     * $condition['offset'] = ?
     * $condition['per_page'] = ?
     * @param $condition
     * @param $row_str_FLAG 仅对查询结果是单条的：若设为TRUE：单条自动转为字符串， 若设为FALSE：单条自动转为数组
     * @param $count_FLAG 若设为TRUE：计算查询结果数， 若设为FALSE：直接查询结果
     * @param $select_key 留空为查询全部键，否则查询指定键
     */
    function search_by_condition($condition, $row_str_FLAG = FALSE, $count_FLAG = FALSE, $select_key = '') {
    	// Defalut Value
    	if (!isset($condition['is_visible'])) {
    		$condition['is_visible'] = TRUE;
    	}
    	if (!isset($condition['sort_weight'])) {
    		$condition['sort_weight'] = 'ASC';
    	}
    	
		if ($select_key != '') {
			$this->db->select($select_key);
		}

    	if (isset($condition['id'])) {
            $this->db->where('id', $condition['id']);
        }
        
    	if (isset($condition['con_id'])) {
            $this->db->where('con_id', $condition['con_id']);
        }
        
        if (isset($condition['type'])) {
            $this->db->where('type', $condition['type']);
        }

        if (isset($condition['year'])) {
            $this->db->where('year', $condition['year']);
        }
        
    	if (isset($condition['name'])) {
            $this->db->where('name', $condition['name']);
        }
        
    	if (isset($condition['content'])) {
            $this->db->where('content', $condition['content']);
        }
        
    	if (isset($condition['is_visible'])) {
    		$temp = ($condition['is_visible']) ? 1 : 0;
            $this->db->where('is_visible', $temp);
        }

        if (!$count_FLAG) {
        	if (isset($condition['sort_year'])) {
                $this->db->order_by("year", $condition['sort_year']);
            }
            
            if (isset($condition['sort_weight'])) {
            	$this->db->order_by("weight", $condition['sort_weight']);
            }
            
            if (isset($condition['sort_create_time'])) {
                $this->db->order_by("create_time", $condition['sort_create_time']);
            }
            
        	if (isset($condition['sort_update_time'])) {
                $this->db->order_by("update_time", $condition['sort_update_time']);
            }

            if (isset($condition['offset'])) {
                if (isset($condition['per_page'])) {
                    $this->db->limit($condition['per_page'], $condition['offset']);
                } else {
                    $this->db->limit(PER_PAGE, $condition['offset']);
                }
            }

            $query = $this->db->get('con_common');
        	if ($query->num_rows() == 1) {
				if (!$row_str_FLAG) {
	            	return $query->result_array();
				} else {
					return $query->row_array();
            	}
			} elseif ($query->num_rows() > 1) {
				return $query->result_array();
			} else {
				return NULL;
			}
        } else {
            $this->db->from('con_common');
            return $this->db->count_all_results();
        }
    }
    
	function _get_all_year_by_type($type) {
    	$query = $this->db->query("SELECT DISTINCT year FROM {$this->table_name} WHERE year != '' AND type = '{$type}' ORDER BY year DESC");
		$temp_data = $query->result_array();
		foreach ($temp_data as $v) {
			//if ($v['year']) {
				$data[] = $v['year'];
			//}
		}
		return $data;
    }
}